base_url = "http://127.0.0.1:7861/chat"
data = {
    "model": "qwen1.5-chat",
    "messages": [
        {"role": "user", "content": "你好"},
        {"role": "assistant", "content": "你好，我是人工智能大模型"},
        {"role": "user", "content": "请用100字左右的文字介绍自己"},
    ],
    "stream": True,
    "temperature": 0.7,
}

# 方式一：使用 requests
import requests
response = requests.post(f"{base_url}/chat/completions", json=data, stream=True)
for line in response.iter_content(None, decode_unicode=True):
    print(line, end="", flush=True)

# 方式二：使用 openai sdk
import openai
client = openai.Client(base_url=base_url, api_key="EMPTY")
resp = client.chat.completions.create(**data)
for r in resp:
    print(r)